package com.jabo.douban.demo.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.text.Html;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.TextView;

import com.jabo.douban.demo.R;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author LanFeng
 * @date 2020/3/5.
 * email：lanfeng0318@163.com
 * description：Html加载图片
 **/
public class MyImageGetter implements Html.ImageGetter {
    WeakReference<TextView> mTextViewReference;
    Context mContext;
    private TextView textView;

    public MyImageGetter(Context context, TextView textView) {
        mContext = context.getApplicationContext();
        mTextViewReference = new WeakReference<TextView>(textView);
        this.textView = textView;
    }

    @Override
    public Drawable getDrawable(String url) {
        URLDrawable urlDrawable = new URLDrawable(mContext);
//         异步获取图片，并刷新显示内容
        new ImageGetterAsyncTask(url, urlDrawable).execute();
        return urlDrawable;
    }

    public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
        WeakReference<URLDrawable> mURLDrawableReference;
        String mUrl;

        public ImageGetterAsyncTask(String url, URLDrawable drawable) {
            mURLDrawableReference = new WeakReference<>(drawable);
            mUrl = url;
        }

        @Override
        protected Drawable doInBackground(String... params) {
            // 下载图片，并且使用缓存
            Bitmap bitmap = returnBitmap(mUrl);
            BitmapDrawable bitmapDrawable = new BitmapDrawable(mContext.getResources(), bitmap);

            DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
            int w = displayMetrics.widthPixels - 100;
            Rect bounds;
            int lineCount = mTextViewReference.get().getLineCount();
            int lineHeight = mTextViewReference.get().getLineHeight();

            Log.e("zjb", "doInBackground: " + lineCount + "  " + lineHeight);

            if (bitmap.getWidth() > w) {
                bounds = new Rect(0, lineCount * lineHeight, w, w * bitmap.getHeight() / bitmap.getWidth());
            } else {
                bounds = new Rect(0, lineCount * lineHeight, bitmap.getWidth(), bitmap.getHeight());
            }
            if (mURLDrawableReference.get() != null) {
                mURLDrawableReference.get().setBounds(bounds);
            }
            bitmapDrawable.setBounds(bounds);
            return bitmapDrawable;
        }

        @Override
        protected void onPostExecute(Drawable result) {
            if (null != result) {
                if (mURLDrawableReference.get() != null) {
                    mURLDrawableReference.get().drawable = result;
                }
                if (mTextViewReference.get() != null) {
                    // 加载完一张图片之后刷新显示内容
                    mTextViewReference.get().setText(mTextViewReference.get().getText());
                }
            }
        }
    }

    private Bitmap returnBitmap(String url) {
        URL fileUrl = null;
        Bitmap bitmap = null;

        try {
            fileUrl = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        try {
            HttpURLConnection conn = (HttpURLConnection) fileUrl
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;

    }

    public class URLDrawable extends BitmapDrawable {
        protected Drawable drawable;

        public URLDrawable(Context context) {
            // 设置默认大小和默认图片
            Rect bounds = new Rect(0, 0, 50, 50);
            setBounds(bounds);
            drawable = context.getResources().getDrawable(R.drawable.afds);
            drawable.setBounds(bounds);
        }

        @Override
        public void draw(Canvas canvas) {
            if (drawable != null) {
                drawable.draw(canvas);
            }
        }
    }
}
